﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Office.Interop.Word;

namespace WFL.Office.Word
{
    public class WordDocument
    {
        private readonly object _lock = new object();

        private WordParagraphs _paragraphs;
        public WordParagraphs Paragraphs
        {
            get
            {
                if (_paragraphs == null)
                {
                    lock (_lock) { if (_paragraphs == null) { _paragraphs = new WordParagraphs(_document.Paragraphs) { Document = this }; } }
                }
                return _paragraphs;
            }
        }

        private WordInlineShapes _inlineShapes;
        public WordInlineShapes InlineShapes
        {
            get
            {
                if (_inlineShapes == null)
                {
                    lock (_lock) { if (_inlineShapes == null) { _inlineShapes = new WordInlineShapes(_document.InlineShapes) { Document = this }; } }
                }
                return _inlineShapes;
            }
        }

        private WordStyles _styles;
        public WordStyles Styles
        {
            get
            {
                if (_styles == null)
                {
                    lock (_lock) { if (_styles == null) { _styles = new WordStyles(_document.Styles); } }
                }
                return _styles;
            }
        }

        private WordSections _sections;
        public WordSections Sections
        {
            get
            {
                if (_sections == null)
                {
                    lock (_lock) { if (_sections == null) { _sections = new WordSections(_document.Sections) { Document = this }; } }
                }
                return _sections;
            }
        }

        private WordBookmarks _bookmarks;
        public WordBookmarks Bookmarks
        {
            get
            {
                if (_bookmarks == null)
                {
                    lock (_lock) { if (_bookmarks == null) { _bookmarks = new WordBookmarks(_document.Bookmarks) { Document = this }; } }
                }
                return _bookmarks;
            }
        }

        private WordPageSetup _pageSetup;
        public WordPageSetup PageSetup
        {
            get
            {
                if (_pageSetup == null)
                {
                    lock (_lock) { if (_pageSetup == null) { _pageSetup = new WordPageSetup(_document.PageSetup) { Document = this }; } }
                }
                return _pageSetup;
            }
        }

        private WordHyperlinks _hyperlinks;
        public WordHyperlinks Hyperlinks
        {
            get
            {
                if (_hyperlinks == null)
                {
                    lock (_lock) { if (_hyperlinks == null) { _hyperlinks = new WordHyperlinks(_document.Hyperlinks) { Document = this }; } }
                }
                return _hyperlinks;
            }
        }

        private WordShapes _shapes;
        public WordShapes Shapes
        {
            get
            {
                if (_shapes == null)
                {
                    lock (_lock) { if (_shapes == null) { _shapes = new WordShapes(_document.Shapes); } }
                }
                return _shapes;
            }
        }

        private WordRange _content;
        public WordRange Content
        {
            get
            {
                if (_content == null)
                {
                    lock (_lock) { if (_content == null) { _content = new WordRange(_document.Content); } }
                }
                return _content;
            }
        }

        private WordComments _comments;
        public WordComments Comments
        {
            get
            {
                if (_comments == null)
                {
                    lock (_lock) { if (_comments == null) { _comments = new WordComments(_document.Comments); } }
                }
                return _comments;
            }
        }

        public WordApplication Application
        {
            get;
            internal set;
        }

        private Document _document;

        internal WordDocument(Document document)
        {
            _document = document;
        }

        public WordRange Range(int? start = null, int? end = null)
        {
            object start1 = start ?? Type.Missing;
            object end1 = end ?? Type.Missing;
            return new WordRange(_document.Range(ref start1, ref end1)) { Document = this };
        }

        public void SaveAs(string path)
        {
#if Word2003
            _document.SaveAs(path, WdSaveFormat.wdFormatDocument);
#else
            _document.SaveAs(path, WdSaveFormat.wdFormatDocumentDefault);
#endif
        }

        public void SaveAsText(string path, Encoding encoding)
        {
            _document.SaveAs(path, WdSaveFormat.wdFormatText
                , Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing
                , Type.Missing, Type.Missing, Type.Missing, Type.Missing, encoding.CodePage
                , Type.Missing, Type.Missing, WdLineEndingType.wdCRLF, Type.Missing);
        }

        public void Save()
        {
            _document.Save();
        }

        public void Close()
        {
            _document.Close();
        }


        

    }
}
